package cn.tedu.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GeoService implements CommandLineRunner {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public void addLocation(String key,String name,double x,double y){
        redisTemplate.opsForGeo().add(key,new Point(x,y),name);
    }
    /**查找附近的地址*/
    public GeoResults<RedisGeoCommands.GeoLocation<Object>> findNearbyLocations(
            String key,
            double x,
            double y,
            Distance distance){
        Circle circle = new Circle(new Point(x, y), distance);
        GeoResults<RedisGeoCommands.GeoLocation<Object>> radius
                = redisTemplate.opsForGeo().radius(key, circle);
        return radius;
    }

    /**
     * 设置一些初始数据
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        System.out.println("run(String... args)");
        addLocation("location","hotel-a",116.3975,39.9087);
        addLocation("location","hotel-b",116.3875,39.9087);
        addLocation("location","hotel-c",116.3775,39.9087);
    }
}
